package org.example.library;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;
import android.widget.Toast;
import org.example.reservas_mesas.*;


public class MyBinder implements ViewBinder {

	Context MyContext;
        String user;
        Activity act;

	
	public MyBinder(Context contexto,String user,Activity act){
		MyContext = contexto;
                this.user = user;
                this.act = act;
	}
        
        
	public boolean setViewValue(View view, Cursor arg1, int arg2) {
                if(view.getId() == R.id.cancelar)
                         {
                     final Button b=(Button) view;

                     b.setOnClickListener(new Button.OnClickListener() {  
                         public void onClick(View v){
                            Toast toast1 = Toast.makeText(MyContext.getApplicationContext(),"Vale" + b.getTag(), Toast.LENGTH_LONG);
                             toast1.show();
                         }
                      });
                     return true; 
                }
		
		if(view.getId() == R.id.estado_lista_reserva)
		{
			final TextView texto = (TextView) view;
			String prueba =  texto.getText().toString(); 
                        Toast toast1 = Toast.makeText(MyContext.getApplicationContext(),"Vale " + texto.getTag() + " - " + texto.getText(), Toast.LENGTH_LONG);
                        toast1.show();
                        
			if(texto.getTag().toString().equals("0"))
			{
				texto.setText("Pendiente");
			}
			else if(texto.getTag().toString().equals("1"))
			{
				texto.setText("Aceptada");
				View padre =  (View) (view.getParent());
				padre.findViewById(R.id.cancelar).setVisibility(view.INVISIBLE);
			}
			else
			{
				texto.setText("Cancelada");
				View padre =  (View) (view.getParent());
				padre.findViewById(R.id.cancelar).setVisibility(view.INVISIBLE);
			}
			return true;
		}
        return false;
	}

	@Override
	public boolean setViewValue(View view, Object data, String textRepresentation) {
		
            if(view.getId() == R.id.cancelar){
                final Button b=(Button) view;
            
                b.setOnClickListener(new Button.OnClickListener() {  
                    public void onClick(View v)
                    {
                        Invoker invoker = new Invoker();
                        ServidorPHP servidor = new ServidorPHP();
                        servidor.SetIDReserva(""+b.getTag());
                        CancelarReserva CR = new CancelarReserva(servidor);

                        invoker.SetPeticion(CR);
                        invoker.Ejecuta();

                        Intent i = new Intent(act,listado.class);
                        i.putExtra("usuario",user);
                        act.startActivity(i);                  
                    }
                 });
                return true; 
            }
		
            if(view.getId() == R.id.estado_lista_reserva){
                    final TextView texto = (TextView) view;


                    if(textRepresentation.equals("0"))
                    {
                            texto.setTextColor(Color.YELLOW);
                            texto.setText("Pendiente");
                    }
                    else if(textRepresentation.equals("1"))
                    {
                            texto.setTextColor(Color.GREEN);
                            texto.setText("Aceptada");
                            View padre =  (View) (view.getParent());
                            padre.findViewById(R.id.cancelar).setVisibility(view.INVISIBLE);
                    }
                    else
                    {
                            texto.setTextColor(Color.RED);
                            texto.setText("Cancelada");
                            View padre =  (View) (view.getParent());
                            padre.findViewById(R.id.cancelar).setVisibility(view.INVISIBLE);
                    }
                    return true;
            }
        return false;
	}

}
